<template>
  <div class="video-box">
    <div v-if="!data" class="no-video video-desc-box">
      <i class="el-icon-video-camera"></i>
    </div>
    <template v-else>
      <div v-if="errorExceed" class="video-desc-box">
        该视频暂时无法查看,请查看切换视频
      </div>
      <div v-if="videoOffline" class="video-offline video-desc-box">
        <el-image :src="videoOfflineIcon"></el-image>
        <div class="text">视频离线</div>
      </div>
      <div class="video-main" v-if="videoOnline">
        <div class="video" ref="video"></div>
        <div class="video-info" v-if="showVideoInfo">
          <div class="left">
            {{ data.machineName }}
          </div>
          <div class="right">
            <el-image
              class="status-img"
              v-if="statusIcon"
              :src="statusIcon"
              fit="contain"
            ></el-image>
            {{ statusText }}
            <i
              v-if="showCloseIcon"
              @click="handleCloseVideo"
              class="el-icon-close icon"
            ></i>
          </div>
        </div>
      </div>
    </template>
  </div>
</template>

<script>
// 如果视频没显示 请设置父级高度
// 如果视频没显示 请设置父级高度
// 如果视频没显示 请设置父级高度

// 视频-正常
import videoNormalIcon from "@/assets/icon_camera_online.png";
// 视频-离线
import videoOfflineIcon from "@/assets/icon_camera_offline.png";
// 视频-报警
import videoAlarmIcon from "@/assets/icon_camera_alarm.png";

// import 'xgplayer';
// import FlvPlayer from 'xgplayer-flv';
import FlvJsPlayer from 'xgplayer-flv.js';
import {getVideoUrl} from "@/api/video.js"; 
// import { videoBaseUrl } from '@/config';

const iconEnum = {
  1: {
    icon: videoNormalIcon,
    text: "在线",

  },
  2: {
    icon: videoOfflineIcon,
    text: "离线",
  },
};
const videoStatusEnum = {
  online: {
    status: "1",
    text: "在线",
  },
  offline: {
    status: "2",
    text: "离线",
  },
};

export default {
  name: "v-video",
  components: {},
  props: {
    showVideoInfo: {
      type: Boolean,
      default: false,
    },
    showCloseIcon: {
      type: Boolean,
      default: false,
    },
    data: {
      type: Object,
      default() {
        return {};
      },
    },
    playerConf: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  data() {
    return {
      videoOfflineIcon,
      player: null,
      errorTimes: 0,
    };
  },
  computed: {
    videoOnline() {
      if (!this.data) {
        return false;
      }
      const { onlineStatus = videoStatusEnum.offline.status } = this.data;
      return String(onlineStatus) !== videoStatusEnum.offline.status;
    },
    videoOffline() {
      if (!this.data) {
        return false;
      }
      const { onlineStatus = videoStatusEnum.offline.status } = this.data;
      return String(onlineStatus) === videoStatusEnum.offline.status;
    },
    statusText() {
      if (!this.data) {
        return "";
      }
      const { onlineStatus = videoStatusEnum.offline.status } = this.data;
      return iconEnum[onlineStatus].text;
    },
    statusIcon() {
      if (!this.data) {
        return "";
      }
      const { onlineStatus = videoStatusEnum.offline.status } = this.data;
      return iconEnum[onlineStatus].icon;
    },
    errorExceed() {
      return this.errorTimes >= 3;
    },
  },
  watch: {
    data: {
      handler(newVal, oldVal) {
        if (newVal === oldVal) {
          return newVal;
        }
        if (!newVal) {
          this.destroyVideo();
          return newVal;
        }
        this.$nextTick(() => {
          this.initVideo(newVal);
        });
      },
      immediate: true,
    },
  },
  mounted() {
    // console.log('如果视频没显示 请设置父级高度');
  },
  methods: {
    handleCloseVideo() {
      this.$emit("close-video", this.data);
    },
    async initVideo(data = this.data) {
      if (String(data.onlineStatus) !== videoStatusEnum.online.status) {
        return false;
      }

      const urlRes = await getVideoUrl({
        machineNum: data.machineNum
      })
      const url = urlRes.data.wsFlv

      if (this.player) {
        this.player.src = url;
      } else {
        this.player = new FlvJsPlayer({
          el: this.$refs.video,
          url,
          autoplay: true,
          autoplayMuted: true,
          cors: false,
          screenShot: true,
          isLive: true,
          fluid: true,
          retryTimes: 3,
          ignores: ["volume"],
          ...this.playerConf,
        });
      }

      this.player.on("error", (e)=> {
        console.error("发生了错误", e);
        if (this.errorExceed) {
        } else {
          // this.initVideo();
        }
      });
    },
    destroyVideo() {
      return new Promise((resolve) => {
        this.player?.destroy();
        setTimeout(() => {
          this.player = null;
          resolve();
        });
      });
    },
  },
  async beforeDestroy() {
    await this.destroyVideo();
  },
};
</script>

<style lang="scss" scoped>
.video-box {
  width: 100%;
  height: 100%;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  &:hover {
    .video-main {
      .video-info {
        .right {
          .icon {
            opacity: 1;
          }
        }
      }
    }
  }

  .video-desc-box {
    position: absolute;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: #000000;
    font-size: 16px;
    z-index: 999;
    color: #fff;
    i {
      font-size: 58px;
      color: #fff;
    }
    .text {
      margin-left: 10px;
    }
  }
  // .video-offline{

  // }
  // .no-video {
  // }
  .video-main {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    .video {
      max-width: 100%;
      max-height: 100%;
    }
    .video-info {
      position: absolute;
      left: 0;
      top: 0;
      right: 0;
      height: 38px;
      line-height: 38px;
      padding: 0 19px;
      font-size: 14px;
      color: #fff;
      cursor: default;
      display: flex;
      align-items: center;
      justify-content: space-between;
      background-color: rgba(0, 0, 0, 0.6);
      z-index: 999;
      // .left{
      // }
      .right {
        height: 38px;
        line-height: 38px;
        display: flex;
        align-items: center;
        .status-img {
          width: 13px;
          height: 15px;
          margin-right: 7px;
        }
        .icon {
          transition: all 0.3s;
          opacity: 0;
          cursor: pointer;
          font-size: 24px;
          padding: 5px;
        }
      }
    }
    ::v-deep {
      .xgplayer-screenshot {
        .name {
          span {
            display: none;
          }
          &:after {
            content: "抓拍";
            width: fit-content;
            padding: 0 10px;
            height: 20px;
            line-height: 20px;
            background: rgba(0, 0, 0, 0.38);
            border-radius: 10px;
          }
        }
      }
    }
  }
}
</style>
